
package com.crowdfunding.circle.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crowdfunding.circle.common.controller.BaseController;
import com.crowdfunding.circle.common.param.SessionUserInfo;
import com.crowdfunding.circle.project.entity.ProjectVO;
import com.crowdfunding.circle.project.param.ProjectParam;
import com.crowdfunding.circle.project.service.ProjectService;

/**
 * 项目详情Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class ProjectController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Autowired
    private ProjectService projectService;

    private final static String ADMIN_LIST_PAGE = "/admin/project_manage";
    
    private final static String LIST_PAGE = "/project/project_list_ajax";

    private final static String UPDATE_PAGE = "/project/project_edit";
    
    @RequestMapping(value = "/admin/project/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectVO projectVO = new ProjectVO();
        resultMap.put("project", projectVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/project/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ProjectVO projectVO = projectService.findById(id);
        resultMap.put("project", projectVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/project/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(ProjectVO projectVO) {

        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");

        Map<String, Object> resultMap = new HashMap<String, Object>();

        String validMsg = projectVO.valid();
        if (StringUtils.isNotBlank(validMsg)) {
            resultMap.put("result", false);
            resultMap.put("message", validMsg);
            return JSONObject.toJSONString(resultMap);
        }

        try {
            Integer affectCount = 0;
            
            if (projectVO != null && StringUtils.isNotBlank(projectVO.getProjectId())) {
                affectCount = projectService.update(projectVO);
            } else if (projectVO != null) {
                projectVO.setProjectId(UUID.randomUUID().toString());
                projectVO.setPublishUser(seUser.getUserId());
                projectVO.setDataStatus(0);
                affectCount = projectService.insert(projectVO);
            }
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改项目详情成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改项目详情失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改项目详情出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改项目详情出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/admin/project/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = projectService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除项目详情成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除项目详情失败");
            }
        } catch (Exception e) {
            logger.error("删除项目详情出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除项目详情出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/project/list")
    public ModelAndView queryAdminList(ProjectParam projectParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ProjectVO> dataList = projectService.queryByPage(projectParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", projectParam);
        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/uncheck/project/list")
    public ModelAndView queryList(ProjectParam projectParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<ProjectVO> dataList = projectService.queryByPage(projectParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", projectParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
}
